package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.annotations.Param;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.SysNoticeReceiver;
import com.ruoyi.system.domain.SysNoticeRead;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.ISysNoticeReceiverService;
import com.ruoyi.system.service.ISysNoticeReadService;

/**
 * 公告 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;

    @Autowired
    private ISysNoticeReceiverService noticeReceiverService;

    @Autowired
    private ISysNoticeReadService noticeReadService;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        SysNotice notice = noticeMapper.selectNoticeById(noticeId);
        if (notice != null) {
            // 查询接收者信息
            List<SysNoticeReceiver> receivers = noticeReceiverService.selectNoticeReceiverByNoticeId(noticeId);
            List<Long> deptIds = new ArrayList<>();
            List<Long> userIds = new ArrayList<>();
            
            for (SysNoticeReceiver receiver : receivers) {
                if ("1".equals(receiver.getReceiverType())) {
                    deptIds.add(receiver.getReceiverId());
                } else if ("2".equals(receiver.getReceiverType())) {
                    userIds.add(receiver.getReceiverId());
                }
            }
            
            notice.setDeptIds(deptIds);
            notice.setUserIds(userIds);
            
            // 查询阅读统计信息
            SysNoticeRead statistics = noticeReadService.selectNoticeReadStatistics(noticeId);
            if (statistics != null) {
                notice.setTotalCount(statistics.getTotalCount());
                notice.setReadCount(statistics.getReadCount());
                notice.setUnreadCount(statistics.getUnreadCount());
            }
        }
        return notice;
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        List<SysNotice> notices = noticeMapper.selectNoticeList(notice);
        for (SysNotice n : notices) {
            // 查询阅读统计信息
            SysNoticeRead statistics = noticeReadService.selectNoticeReadStatistics(n.getNoticeId());
            if (statistics != null) {
                n.setTotalCount(statistics.getTotalCount());
                n.setReadCount(statistics.getReadCount());
                n.setUnreadCount(statistics.getUnreadCount());
            }
        }
        return notices;
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNotice(SysNotice notice)
    {
        int result = noticeMapper.insertNotice(notice);
        
        if (result > 0 && notice.getNoticeId() != null) {
            // 保存接收者信息
            saveNoticeReceivers(notice);
        }
        
        return result;
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNotice(SysNotice notice)
    {
        int result = noticeMapper.updateNotice(notice);
        
        if (result > 0) {
            // 删除原有接收者信息
            noticeReceiverService.deleteNoticeReceiverByNoticeId(notice.getNoticeId());
            // 保存新的接收者信息
            saveNoticeReceivers(notice);
        }
        
        return result;
    }

    /**
     * 删除公告对象
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNoticeById(Long noticeId)
    {
        // 删除接收者信息
        noticeReceiverService.deleteNoticeReceiverByNoticeId(noticeId);
        // 删除阅读记录
        noticeReadService.deleteNoticeReadByNoticeId(noticeId);
        // 删除公告
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        for (Long noticeId : noticeIds) {
            // 删除接收者信息
            noticeReceiverService.deleteNoticeReceiverByNoticeId(noticeId);
            // 删除阅读记录
            noticeReadService.deleteNoticeReadByNoticeId(noticeId);
        }
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    /**
     * 保存公告接收者信息
     * 
     * @param notice 公告信息
     */
    private void saveNoticeReceivers(SysNotice notice)
    {
        List<SysNoticeReceiver> receivers = new ArrayList<>();
        
        // 保存部门接收者
        if (notice.getDeptIds() != null && !notice.getDeptIds().isEmpty()) {
            for (Long deptId : notice.getDeptIds()) {
                SysNoticeReceiver receiver = new SysNoticeReceiver();
                receiver.setNoticeId(notice.getNoticeId());
                receiver.setReceiverType("1"); // 部门
                receiver.setReceiverId(deptId);
                receivers.add(receiver);
            }
        }
        
        // 保存用户接收者
        if (notice.getUserIds() != null && !notice.getUserIds().isEmpty()) {
            for (Long userId : notice.getUserIds()) {
                SysNoticeReceiver receiver = new SysNoticeReceiver();
                receiver.setNoticeId(notice.getNoticeId());
                receiver.setReceiverType("2"); // 用户
                receiver.setReceiverId(userId);
                receivers.add(receiver);
            }
        }
        
        if (!receivers.isEmpty()) {
            noticeReceiverService.batchInsertNoticeReceiver(receivers);
        }
    }

    /**
     * 查询用户未读公告列表
     * 
     * @param userId 用户ID
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectUnreadNoticesByUserId(Long userId)
    {
        return noticeMapper.selectUnreadNoticesByUserId(userId);
    }

    /**
     * 标记公告为已读
     * 
     * @param noticeId 公告ID
     * @param userId 用户ID
     * @return 结果
     */
    public int markNoticeAsRead(@Param("noticeId") Long noticeId, @Param("userId") Long userId)
    {
        return noticeReadService.markNoticeAsRead(noticeId, userId);
    }

    /**
     * 查询用户相关的通知公告列表
     * 
     * @param userId 用户ID
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectUserNoticesByUserId(Long userId)
    {
        return noticeMapper.selectUserNoticesByUserId(userId);
    }
}
